package com.lantosec.hmrp.modules.eval.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.core.shiro.TokenUtil;
import com.lantosec.hmrp.core.util.DateUtils;
import com.lantosec.hmrp.modules.eval.entity.*;
import com.lantosec.hmrp.modules.eval.mapper.EvalPlanMapper;
import com.lantosec.hmrp.modules.eval.service.IEvalPlanService;
import com.lantosec.hmrp.modules.eval.vo.EvalPlanVO;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 考评计划 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-03-17
 */
@Service
@Primary
@Log4j2
public class EvalPlanServiceImpl extends ServiceImpl<EvalPlanMapper, EvalPlan> implements IEvalPlanService {


    @Override
    public List<EvalPlanVO> queryAll(PageVO<EvalPlanVO> page,String startDate, String endDate, String evalPlanName, Integer evalPlanCycle, Integer evalPlanType, Integer evalMode, Integer state) {
        List<EvalPlanVO> evalPlanVOList = baseMapper.queryAll(page,startDate, endDate, evalPlanName, evalPlanCycle, evalPlanType, evalMode, state);
        page.setRecords(evalPlanVOList);
        return evalPlanVOList;
    }

    @Override
    public Boolean saveAll(EvalPlanList evalPlanList) {
        //保存生成计划信息
        EvalPlan evalPlan = saveEvalPlan(evalPlanList);
        //判断是否是临时计划，临时计划新建即初始化
        if (evalPlan.getEvalPlanCycle() == 0) {
            //初始化巡查临时计划
            if(evalPlan.getEvalMode() == 2){
                this.queryPlanList(evalPlan.getEvalPlanId());
            }

        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    public EvalPlan saveEvalPlan(EvalPlanList evalPlanList) {
        //新增考评计划基本信息
        EvalPlan evalPlan = evalPlanList.getEvalPlan();
        //获取当前用户id和名称
        String userId = TokenUtil.getUserId();
        String userName = TokenUtil.getName();
        evalPlan.setCreateUserId(userId);
        evalPlan.setCreateUser(userName);
        evalPlan.setEvalPlanType(1);
        evalPlan.setCreateTime(DateUtils.nowDate());
        evalPlan.setState(2);
        this.save(evalPlan);
        //新增考评计划明细
        List<EvalPlanItem> evalPlanItemList = evalPlanList.getEvalPlanItemList();
        if (null != evalPlanItemList && evalPlanItemList.size() > 0) {
            evalPlanItemList.forEach(parm -> {
                parm.setEvalPlanId(evalPlan.getEvalPlanId());
                baseMapper.saveEvalPlanItem(parm);
            });
        }
        //新增考评计划执行者
        List<EvalPlanExecutor> evalPlanExecutorList = evalPlanList.getEvalPlanExecutorList();
        if (null != evalPlanExecutorList && evalPlanExecutorList.size() > 0) {
            evalPlanExecutorList.forEach(parm -> {
                parm.setEvalPlanId(evalPlan.getEvalPlanId());
                //如果执行对象是岗位
                if(parm.getExecutorMode() == 2){
                    String[] executorDeptId = parm.getExecutorDeptId().split(",");
                    for(int i=0;i<executorDeptId.length;i++){
                        parm.setExecutorDeptId(executorDeptId[i]);
                        parm.setExecutorPositionId(parm.getExecutorId());
                        parm.setExecutorId(null);
                        baseMapper.saveEvalPlanExecutor(parm);
                    }
                }
                //如果执行对象是人员
                if(parm.getExecutorMode() == 3){
                    String[] executorId = parm.getExecutorId().split("-");
                    parm.setExecutorDeptId(executorId[0]);
                    parm.setExecutorId(executorId[1]);
                    baseMapper.saveEvalPlanExecutor(parm);
                }
            });
        }
        //新增考评计划考评对象
        List<EvalPlanTarget> evalPlanTargetList = evalPlanList.getEvalPlanTargetList();
        if (null != evalPlanTargetList && evalPlanTargetList.size() > 0) {
            evalPlanTargetList.forEach(parm -> {
                parm.setEvalPlanId(evalPlan.getEvalPlanId());
                //如果考评对象是部门
                if(parm.getTargetMode() == 1){
                    parm.setDeptId(parm.getTargetId());
                }
                //如果考评对象是岗位
                if(parm.getTargetMode() == 2){
                    String[] targetId = parm.getTargetId().split("-");
                    parm.setDeptId(targetId[0]);
                    parm.setPositionId(targetId[1]);
                }
                //如果考评对象是人员
                if(parm.getTargetMode() == 3){
                    String[] targetId = parm.getTargetId().split("-");
                    parm.setDeptId(targetId[0]);
                    parm.setUserId(targetId[1]);
                }
                baseMapper.saveEvalPlanTarget(parm);
            });
        }
        return evalPlan;
    }

    @Override
    public void updateAll(EvalPlanList evalPlanList) {
        //修改考评计划信息
        EvalPlan evalPlan = updateEvalPlan(evalPlanList);
        //判断是否是临时计划，临时计划新建即初始化
        if (evalPlan.getEvalPlanCycle() == 0) {
            //初始化临时计划
            this.queryPlanList(evalPlan.getEvalPlanId());
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public EvalPlan updateEvalPlan(EvalPlanList evalPlanList) {
        //考评计划基本信息
        EvalPlan evalPlan = evalPlanList.getEvalPlan();
        this.updateById(evalPlan);
        //考评计划项目明细
        Integer evalPlanId = evalPlan.getEvalPlanId();
        List<EvalPlanItem> evalPlanItemList = evalPlanList.getEvalPlanItemList();
        if (null != evalPlanItemList && evalPlanItemList.size() > 0) {
            //修改--先删后增
            baseMapper.deleteEvalPlanItem(evalPlanId);
            evalPlanItemList.forEach(parm -> {
                parm.setEvalPlanId(evalPlanId);
                baseMapper.saveEvalPlanItem(parm);
            });
        }
        //考评计划执行者信息
        List<EvalPlanExecutor> evalPlanExecutorList = evalPlanList.getEvalPlanExecutorList();
        if (null != evalPlanExecutorList && evalPlanExecutorList.size() > 0) {
            //修改--先删后增
            baseMapper.deleteEvalPlanExecutor(evalPlanId);
            evalPlanExecutorList.forEach(parm -> {
                //如果执行对象是岗位
                if(parm.getExecutorMode() == 2){
                    String[] executorId = parm.getExecutorId().split(",");
                    parm.setExecutorId(null);
                    parm.setExecutorDeptId(executorId[0]);
                    parm.setExecutorPositionId(executorId[1]);
                }
                //如果执行对象是人员
                if(parm.getExecutorMode() == 3){
                    String[] executorId = parm.getExecutorId().split("-");
                    parm.setExecutorDeptId(executorId[0]);
                    parm.setExecutorId(executorId[1]);
                }
                parm.setEvalPlanId(evalPlanId);
                baseMapper.saveEvalPlanExecutor(parm);
            });
        }
        //考评计划考评对象
        List<EvalPlanTarget> evalPlanTargetList = evalPlanList.getEvalPlanTargetList();
        if (null != evalPlanTargetList && evalPlanTargetList.size() > 0) {
            //修改--先删后增
            baseMapper.deleteEvalPlanTarget(evalPlanId);
            evalPlanTargetList.forEach(parm -> {
                parm.setEvalPlanId(evalPlanId);
                //如果考评对象是部门
                if(parm.getTargetMode() == 1){
                    parm.setDeptId(parm.getTargetId());
                }
                //如果考评对象是岗位
                if(parm.getTargetMode() == 2){
                    String[] targetId = parm.getTargetId().split("-");
                    parm.setDeptId(targetId[0]);
                    parm.setPositionId(targetId[1]);
                }
                //如果考评对象是人员
                if(parm.getTargetMode() == 3){
                    String[] targetId = parm.getTargetId().split("-");
                    parm.setDeptId(targetId[0]);
                    parm.setUserId(targetId[1]);
                }
                baseMapper.saveEvalPlanTarget(parm);
            });
        }
        return evalPlan;
    }

    @Override
    public EvalPlanList getEvalPlanDetailed(Integer evalPlanId) {
        EvalPlanList evalPlanList = new EvalPlanList();
        //考评计划基本信息
        EvalPlan evalPlan = this.getById(evalPlanId);
        //考评计划详细信息
        List<EvalPlanItem> evalPlanItemList = baseMapper.getEvalPlanDetailed(evalPlanId);
        //考评对象
        List<EvalPlanExecutor> evalPlanExecutorList = getEvalPlanExecutor(evalPlanId);
        evalPlanExecutorList.forEach(param->{
            Integer executorMode = param.getExecutorMode();
            //如果执行对象是崗位
            if(executorMode == 2){
                param.setExecutorId(param.getExecutorPositionId()+"-"+param.getExecutorDeptId());
                param.setName(param.getPositionName()+"-"+param.getDeptName());
            }
            //如果执行对象是人员
            if(executorMode == 3){
                param.setExecutorId(param.getExecutorId()+"-"+param.getExecutorDeptId());
                param.setName(param.getName()+"-"+param.getDeptName());
            }
        });
        //执行对象
        List<EvalPlanTarget> evalPlanTargetList = getEvalPlanTarget(evalPlanId, evalPlan.getTargetMode());
        evalPlanTargetList.forEach(param->{
            Integer targetMode = param.getTargetMode();
            //如果考评对象是部门
            if(targetMode == 1){
                param.setTargetId(param.getDeptId());
                param.setName(param.getDeptName());
            }
            //如果考评对象是崗位
            if(targetMode == 2){
                param.setTargetId(param.getPositionId()+"-"+param.getDeptId());
                param.setName(param.getPositionName()+"-"+param.getDeptName());
            }
            //如果考评对象是人员
            if(targetMode == 3){
                param.setTargetId(param.getUserId()+"-"+param.getDeptId());
                param.setName(param.getName()+"-"+param.getDeptName());
            }
        });
        evalPlanList.setEvalPlan(evalPlan);
        evalPlanList.setEvalPlanItemList(evalPlanItemList);
        evalPlanList.setEvalPlanExecutorList(evalPlanExecutorList);
        evalPlanList.setEvalPlanTargetList(evalPlanTargetList);
        return evalPlanList;
    }

    @Override
    public List<EvalPlanExecutor> getEvalPlanExecutor(Integer evalPlanId) {
        return baseMapper.getEvalPlanExecutor(evalPlanId);
    }

    @Override
    public List<EvalPlanTarget> getEvalPlanTarget(Integer evalPlanId, Integer targetMode) {
        return baseMapper.getEvalPlanTarget(evalPlanId, targetMode);
    }

    @Transactional(rollbackFor = Exception.class)
    //临时计划初始化
    public String queryPlanList(Integer evalPlanId) {
        //查询新建的临时计划
        List<EvalPlan> param = baseMapper.getPlanBaseInfo(evalPlanId);
        if (param.size() > 0) {
            for (EvalPlan evalPlan : param) {
                EvalTask task = new EvalTask();
                BeanUtils.copyProperties(evalPlan, task);
                task.setState(2);
                task.setTaskTitle(evalPlan.getEvalPlanName());
                task.setTargetNum(evalPlan.getTargetNum());
                //初始化时考评对象执行完次数为0
                task.setPerformNum(0);
                //获取当前用户id和名称
                String userId = TokenUtil.getUserId();
                String userName = TokenUtil.getName();
                task.setCreateUserId(userId);
                task.setCreateUser(userName);
                task.setCreateTime(DateUtils.nowDate());
                //初始化考评任务表
                baseMapper.saveEvalTask(task);
                Integer taskId = task.getEvalTaskId();
                //查询考评任务详细信息
                List<EvalTaskItem> result = baseMapper.getPlanDetailInfo(taskId);
                for (EvalTaskItem d : result) {
                    d.setEvalTaskId(taskId);
                    //初始化考评任务明细表
                    baseMapper.saveEvalTaskItem(d);
                }
            }
            return "计划执行成功了>>>>>" + LocalDateTime.now();
        } else {
            return "没有可执行的计划>>>>>" + LocalDateTime.now();
        }
    }


    @Override
    public Result getSysDeptPosition(String[] deptIds, String positionId) {
        List<String> deptList = Arrays.asList(deptIds);
        Integer size = deptList.size();
        String positionIds = baseMapper.getSysDeptPosition(deptList,size,positionId);
        if(StringUtils.isNotEmpty(positionIds)){
            return Result.ok("该岗位是部门下的相同岗位",true);
        }else {
            return Result.ok("该岗位不是部门下的相同岗位",false);
        }
    }
}
